package cn.lee.cplibrary.util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import cn.lee.cplibrary.R;
import cn.lee.cplibrary.init.CPInitializer;

/**
 * Toast短时间内点击多次只显示一次，持续点击后不显示问题解决方案
 * create by ChrisLee on 2021/12/16
 */
public class ToastUtil {
    //	private static Toast mToast;
//	private static Toast toast;
//
//	/**
//	 * function:显示Toast（自定义和原生在此处修改）不含图片
//	 */
//	public static void showToast(Context context, String text) {
//		toast1word(context, text);
////		 toastCommon(context, text);
//	}
//
//	public static void showToast(Context context, int string_id) {
//		toast1word(context, context.getResources().getString(string_id));
//		// toastCommon(context, text);
//	}
//
//	/**
//	 * function:单例显示Toast，不改变Toast的样式
//	 */
//	private static void toastCommon(Context context, String text) {
//		if (mToast == null) {
//			mToast = Toast.makeText(context, text, Toast.LENGTH_SHORT);
//		} else {
//			mToast.setText(text);
//			mToast.setDuration(Toast.LENGTH_SHORT);
//
//		}
//		mToast.show();
//	}
//
//	/**
//	 * function:显示一句话，自定义的Toast
//	 */
//	private static void toast1word(Context context, String text) {
//		LayoutInflater inflater = (LayoutInflater) context
//				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//		View view = inflater.inflate(R.layout.cp_toast_one_word, null);
//		TextView tv_toast1 =  view.findViewById(R.id.tv_toast1);
//		tv_toast1.setText(text);
//		tv_toast1.setGravity(Gravity.CENTER);
//		// tv_toast1.setLayoutParams(new LinearLayout.LayoutParams(
//		// LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//		if (toast == null) {
//			toast = new Toast(context);
//		}
//		toast.setView(view);
//		toast.setDuration(Toast.LENGTH_SHORT);
//		toast.setGravity(Gravity.CENTER, 0, 0);
//		toast.show();
//	}
//
//	/**
//	 * function: 取消Toast
//	 */
//	public static void cancelToast() {
//		if (mToast != null) {
//			mToast.cancel();
//		}
//		if (toast != null) {
//			toast.cancel();
//		}
//	}
    private static String oldMsg;
    private static long oneTime = 0;
    private static long twoTime = 0;
    private static Toast toast = null;

    /**
     * @param context 建议传入BaseApplication即全局Context
     */
    public static void showToast(Context context, String text) {
        CPInitializer.checkInitialization();
        if (toast == null) {
//            toast = Toast.makeText(getContext(), text, Toast.LENGTH_SHORT);
//            toast.setGravity(Gravity.CENTER, 0, 0);
//            toast.show();
            toast = newToast(context, text);
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (text.equals(oldMsg)) {
                if (twoTime - oneTime > 2000) {
                    // 这里是判断toast上一次显示的时间和这次的显示时间如果大于2000，
                    //  则显示新的toast
                    toast.cancel();
//                    toast = Toast.makeText(getContext(), text, Toast.LENGTH_SHORT);
//                    toast.setGravity(Gravity.CENTER, 0, 0);
//                    toast.show();
                    toast = newToast(context, text);
                    oneTime = twoTime;
                }
            } else {
                toast.cancel();
//                toast = Toast.makeText(getContext(), text, Toast.LENGTH_SHORT);
//                toast.setGravity(Gravity.CENTER, 0, 0);
//                toast.show();
                toast = newToast(context, text);
                oldMsg = text;
                oneTime = twoTime;
            }
        }
    }

    /**
     * @param context 建议传入BaseApplication即全局Context
     */
    public static void showToast(Context context, int stringsId) {
        CPInitializer.checkInitialization();
        if (toast == null) {
            toast = Toast.makeText(context, context.getString(stringsId), Toast.LENGTH_SHORT);
            toast.show();
            oneTime = System.currentTimeMillis();
        } else {
            twoTime = System.currentTimeMillis();
            if (context.getString(stringsId).equals(oldMsg)) {
                if (twoTime - oneTime > 2000) {
                    toast.cancel();
                    toast = Toast.makeText(context, context.getString(stringsId), Toast.LENGTH_SHORT);
                    toast.show();
                    oneTime = twoTime;
                }
            } else {
                toast.cancel();
                toast = Toast.makeText(context, context.getString(stringsId), Toast.LENGTH_SHORT);
                oldMsg = context.getString(stringsId);
                toast.show();
                oneTime = twoTime;
            }
        }

    }

    /**
     * function:显示一句话，自定义的Toast
     */
    private static Toast newToast(Context context, String text) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.cp_toast_one_word, null);
        TextView tv_toast1 = view.findViewById(R.id.tv_toast1);
        tv_toast1.setText(text);
        tv_toast1.setGravity(Gravity.CENTER);
        toast = new Toast(context);
        toast.setView(view);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        return toast;
    }

//	public static Context getContext() {
//		return BaseApplication.getContext();
//	}


}
